package com.china08.yxyapi.api.manage.tec.teaching.lecture.chooseprepare;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.api.manage.edu.platform.resource.yxylectureprepack.YxyLecturePrePackStatusController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;
import com.china08.yxyapi.util.RabbitMqSendUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class LectureSchResourceController extends YxyApiBaseController {

    Logger log = Logger.getLogger(YxyLecturePrePackStatusController.class);

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    private YxyLectureResRepository yxyLectureResRepository;

    @Autowired
    private YxyResResourceRepository YxyResResourceRepository;

    @ApiOperation(value = "备课包资源详情")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/resource/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        String userVIP = getProfile(request, response).getResVip();
        List<RespModel> list = new ArrayList<LectureSchResourceController.RespModel>();
        String prepackId = reqModel.prepackId;
        RabbitMqSendUtil.lecturePvStatis(prepackId);
        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository.findById(prepackId);
        Assert.isTrue(null != yxyLecturePrePack, "备课包为空");
        // 备课包所有资源
        List<Res> list1 = yxyLecturePrePack.getResources();

        RespModel respModel = null;
        for (Res res : list1) {
            List<String> quesIds = new ArrayList<String>();
            if (res.getCourseType() == 0) {
                YxyLectureRes findById = yxyLectureResRepository.findByResourceId(res.getResourceId());
                if (findById != null) {
                    quesIds = findById.getQuesIds();
                }
            }
            respModel = new RespModel();
            respModel.resourceId = res.getResourceId();
            respModel.formatId = res.getFormatId();
            respModel.courseType = res.getCourseType();
            respModel.resourceName = res.getResourceName();
            respModel.orderNum = res.getOrderNum();
            respModel.resourceUrl = res.getResourceUrl();
            respModel.quesIds = quesIds;
            YxyResResource yxyResResource = YxyResResourceRepository.findById(res.getResourceId());
            if (null != yxyResResource) {
                respModel.isHighQualityYx = yxyResResource.getIsHighQualityYx();
                respModel.vip = yxyResResource.getIsVip();
            } else {
                respModel.isHighQualityYx = 0;
                respModel.vip = false;
            }
            respModel.userVIP = userVIP;
            list.add(respModel);
        }
        return list;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String prepackId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(prepackId), "备课包为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        public String resourceId;
        public Integer orderNum;
        public String resourceName;
        public String resourceUrl;
        public String formatId;
        // 0 试题 1资源
        public int courseType;
        public List<String> quesIds;
        public boolean vip;
        public Integer isHighQualityYx;
        public String userVIP;
    }
}
